package com.atguigu.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

public class CodeAutomaticGenerator {
    public static void main(String[] args) {
        //构建一个代码生成启动的对象
        AutoGenerator autoGenerator = new AutoGenerator();

        //编写配置
        //1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//当前项目的路径
        globalConfig.setOutputDir("E:/在线教育项目/guli_parent/service/service_statistics/src/main/java");//生成项目的路径
        globalConfig.setAuthor("董万均");//作者信息
        globalConfig.setOpen(false);//是否打开资源管理器，不打开，也就是打开Windows文件夹
        globalConfig.setFileOverride(false);//是否覆盖原来生成的文件，不覆盖
        globalConfig.setServiceName("%sService");//去掉Service层中IService接口中的I
        globalConfig.setIdType(IdType.ID_WORKER_STR);//主键生成策略
        globalConfig.setDateType(DateType.ONLY_DATE);//仅仅使用时间
        globalConfig.setSwagger2(true);//开启swagger配置
        //开启全局配置
        autoGenerator.setGlobalConfig(globalConfig);

        //2、数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8&characterEncoding=utf-8&useUnicode=true&useSSL=false");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("1554289942");
        dataSourceConfig.setDbType(DbType.MYSQL);//数据库类型Mysql
        //开启数据源配置
        autoGenerator.setDataSource(dataSourceConfig);

        //3、包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.atguigu");
        packageConfig.setModuleName("edustatistics");//其实是：com.mybatis.plus.blog
        packageConfig.setEntity("entity");//com.mybatis.plus.blog下的实体类包名pojo
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setController("controller");
        //开启包名配置
        autoGenerator.setPackageInfo(packageConfig);


        //4、策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("statistics_daily");
        strategyConfig.setTablePrefix(packageConfig.getModuleName()+"_");//去掉实体类表前缀
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);//设置包名的命名规则，下划线转驼峰
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);//设置列的命名规则，下划线转驼峰
        strategyConfig.setEntityLombokModel(true);//开启lombok执行
        strategyConfig.setRestControllerStyle(true);//开启controller的restful风格
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setLogicDeleteFieldName("is_deleted");
        //自动填充配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        List<TableFill> fills=new ArrayList<>();
        fills.add(gmtCreate);
        fills.add(gmtModified);
        strategyConfig.setTableFillList(fills);//自动填充配置
        strategyConfig.setControllerMappingHyphenStyle(true);//localhost：8080/hello_id_2开启下划线支持
        autoGenerator.setStrategy(strategyConfig);

        //执行代码生成器
        autoGenerator.execute();

    }
}
